<?php
namespace app\pachong\controller;  // 必须继承于app 第二个是具体的应用名  例 t5/note/tp
use think\Controller;
//use think\Request;


use think\config;


// use think\Db;
class Index2 extends Controller
{
/*
*https://www.hyhthjk.com.cn/note.php/python/Index/index
django 的安装
*/

public function index(){
echo'
<pre>
地址 http://www.hyhthjk.com.cn/note.php/pachong/Index2/index  爬虫
</pre>
';
}

/**
 * 测试类是否已通
 */
public function index2(){
    $this->index3();
    Vendor('PHPCrawl.libs.PHPCrawler#class');
    $mod = new \PHPCrawler;   // 注意反斜杠
    $mod->text('传参测试');

}

public function index3(){
    echo'
    <pre>
    phpcrawl 安装在 t5/vendor 下

    Vendor("PHPCrawl.libs.PHPCrawler#class");
    $mod = new \PHPCrawler;   // 注意反斜杠
    $mod->text("传参测试");
    </pre>
    ';
}

public function index4(){
    $this->index5();
    set_time_limit(10000);
    Vendor('PHPCrawl.libs.PHPCrawler#class');
    $crawler = new \PHPCrawler;   // 注意反斜杠
   
    $starturl = "http://www.hyhthjk.com.cn/note.php/index/Index/index";
    $starturl = "http://www.hyhthjk.com.cn/note.php/index/Index/index?id=681&content=爬虫";
    #$starturl = "http://www.hyhthjk.com.cn/note.php/boot/Index/index6";
    #$crawler->setURL("www.php.net");

    $crawler->setURL($starturl);
    $crawler->addContentTypeReceiveRule("#text/html#");
    
// Ignore links to pictures, dont even request pictures
$crawler->addURLFilterRule("#\.(jpg|jpeg|gif|png)$# i");

// Store and send cookie-data like a browser does
$crawler->enableCookieHandling(true);

// Set the traffic-limit to 1 MB (in bytes,
// for testing we dont want to "suck" the whole site)
//原文1000*1024 改为0，是无限的意思
$crawler->setTrafficLimit(0);

// Thats enough, now here we go
$crawler->go();

// At the end, after the process is finished, we print a short
// report (see method getProcessReport() for more information)
$report = $crawler->getProcessReport();

if (PHP_SAPI == "cli") $lb = "\n";
else $lb = "<br />";
    
echo "Summary:".$lb;
echo "Links followed: ".$report->links_followed.$lb;
echo "Documents received: ".$report->files_received.$lb;
echo "Bytes received: ".$report->bytes_received." bytes".$lb;
echo "Process runtime: ".$report->process_runtime." sec".$lb; 


}


public function index5(){
echo'
<pre>
在opt/lampp/htdocs/t5/vendor/PHPCrawl/libs/PHPCrawler.class.php中 覆盖重写
function handleDocumentInfo($DocInfo) 
{
  // Just detect linebreak for output ("\n" in CLI-mode, otherwise "<br>").
  if (PHP_SAPI == "cli") $lb = "\n";
  else $lb = "<br />";

  // Print the URL and the HTTP-status-Code
  echo "Page requested: ".$DocInfo->url." (".$DocInfo->http_status_code.")".$lb;
  
  // Print the refering URL
  echo "Referer-page: ".$DocInfo->referer_url.$lb;
  
  // Print if the content of the document was be recieved or not
  if ($DocInfo->received == true)
    echo "Content received: ".$DocInfo->bytes_received." bytes".$lb;
  else
    echo "Content not received".$lb; 
  
  // Now you should do something with the content of the actual
  // received page or file ($DocInfo->source), we skip it in this example 
  
  echo $lb;
  
  flush();
} 
//到上面结束
</pre>
<hr/>
';
echo'
<pre>
set_time_limit(10000);
    Vendor("PHPCrawl.libs.PHPCrawler#class");
    $crawler = new \PHPCrawler;   // 注意反斜杠
   
    $starturl = "http://www.hyhthjk.com.cn/note.php/index/Index/index";
    #$crawler->setURL("www.php.net");

    $crawler->setURL($starturl);
    $crawler->addContentTypeReceiveRule("#text/html#");
    
// Ignore links to pictures, dont even request pictures
$crawler->addURLFilterRule("#\.(jpg|jpeg|gif|png)$# i");

// Store and send cookie-data like a browser does
$crawler->enableCookieHandling(true);

// Set the traffic-limit to 1 MB (in bytes,
// for testing we dont want to "suck" the whole site)
$crawler->setTrafficLimit(1000 * 1024);

// Thats enough, now here we go
$crawler->go();

// At the end, after the process is finished, we print a short
// report (see method getProcessReport() for more information)
$report = $crawler->getProcessReport();

if (PHP_SAPI == "cli") $lb = "\n";
else $lb = "<br />";
    
echo "Summary:".$lb;
echo "Links followed: ".$report->links_followed.$lb;
echo "Documents received: ".$report->files_received.$lb;
echo "Bytes received: ".$report->bytes_received." bytes".$lb;
echo "Process runtime: ".$report->process_runtime." sec".$lb; 
</pre>
<hr/>
';



}


}



